package com.lagou.com.lagou.homework.Modules4.item3;

import java.io.*;

public class ThreadPoolCopy {

    public static void copy(File f1, File f2) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        String p1 = f2.getParent();

        try {
            File[] files = f1.listFiles();
            for (File file : files) {
                if (file.isFile()) {
                    //获取file绝对路径；和f2父目录
                    String ap1 = file.getAbsolutePath();

                    //读文件
                    fis = new FileInputStream(ap1);
                    //获取需要写入的字符串，与父目录拼接
                    String str = ap1.substring(3, ap1.length());

                    fos = new FileOutputStream(p1 + str);

                    //写入
                    byte[] bArr = new byte[1024];//1024的倍数
                    int res = 0;
                    while ((res = fis.read(bArr)) != -1) {
                        fos.write(bArr, 0, res);//从0开始到实际大小
                    }
                    System.out.println("文件" + file.getName() + " copy pass!");
                }
                if (file.isDirectory()) {
                    String ap2 = file.getAbsolutePath();
                    String str1 = ap2.substring(3, ap2.length());
                    File file2 = new File(p1 + str1);
                    if (!file2.exists()) {
                        boolean b1 = file2.mkdirs();
                        System.out.println(file2 + " 目录创建成功");
                    }
                    //System.out.println("[" + file.getName() + "]");
                    copy(file, f2);
                    //System.out.println(file.getName());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != fos) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != fis) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

   /* public static void main(String[] args) {

        File f1 = new File("D:\\JavaTest");
        File f2 = new File("I:\\JavaTest");

        if (!f2.exists()) {
            System.out.println(f2.mkdirs() ? "目录创建成功" : "目录创建失败");
        }
        copy(f1, f2);

    }*/
}
